#include "RMRUtil.h"


// Limit val to range -1.0 .. 1.0
float Limit(float val)
{
	return Limit(val, -1.0, 1.0);
}

// Limit val to range min .. max
float Limit(float val, float min, float max)
{
	return (val < min) ?
			min :
			(val > max) ?
			max :
			val;
}

// Linearly scale val from old_min .. old_max to new_min .. new_max
float ScaleRangeToRange(float val, float old_min, float old_max, float new_min, float new_max)
{
	float old_range = old_max - old_min;
	float new_range = new_max - new_min;
	
	// ensure val is in the range old_min .. old_max
	val = Limit(val, old_min, old_max);
	
	
	return new_min + (val - old_min) / old_range * new_range;
}

// Random int between low and high
int RandInt(int low, int high)
{
	return (int) ScaleRangeToRange(
			(float) rand(),
			(float) 0, 		(float) RAND_MAX,
			(float) low,	(float) high);
}

// Random float between low and high
float RandFloat(float low, float high)
{
	return ScaleRangeToRange(
			(float) rand(),
			(float) 0,	(float) RAND_MAX,
			low,		high);
}

// hypotenues of a right triangle
float Hypot(float a, float b)
{
	return sqrt(a*a + b*b);
}
